package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.PracticeBrushTeacher;
import com.xmy.cultivate.entity.PracticePlan;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.service.IPracticeBrushTeacherService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 审核教师表 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-18
 */
@RestController
@RequestMapping("/api/practiceBrushTeacher")
public class PracticeBrushTeacherController extends BaseController{

    @Autowired
    IPracticeBrushTeacherService iPracticeBrushTeacherService;

    @Autowired
    IQuarterService iQuarterService;

    @GetMapping("")
    public Result getPracticeBrushTeacher(@RequestParam(value = "pageNum", defaultValue = "-1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "-1") Integer pageSize,
                                          @RequestParam(value = "yearPart", required = false) String yearPart,
                                          @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                          @RequestParam(value = "schoolId", required = false) String schoolId,
                                          @RequestParam(value = "subjectsId", required = false) String subjectsId,
                                          @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                          @RequestParam(value = "classTypeId", required = false) String classTypeId) {
        IPage<PracticeBrushTeacher> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.select("*,getStaffNameForId( teacher_id ) AS teacher_name");
        wrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        wrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        wrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        wrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        wrapper.eq(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        wrapper.eq(StringUtils.isNotBlank(classTypeId), "course_type_id", classTypeId);

        iPracticeBrushTeacherService.page(iPage, wrapper);

        return new Result(Code.OK, new PageResult<>(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }


    @GetMapping("/getEnterList")
    public Result getEnterList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize",defaultValue = "-1") Integer pageSize,
                               @RequestParam(value = "yearPart",required = false) Integer yearPart,
                               @RequestParam(value = "quarterNum",required = false) Integer quarterNum,
                               @RequestParam(value = "subjectsId",required = false) String subjectsId,
                               @RequestParam(value = "yearClassId",required = false) String yearClassId,
                               @RequestParam(value = "lessonType",required = false,defaultValue = "1") Integer lessonType,
                               @RequestParam(value = "classTypeId",required = false) String classTypeId,
                               @RequestParam(value = "teacherName",required = false) String teacherName,
                               @RequestParam(value = "lessonNumStart",required = false) Integer lessonNumStart,
                               @RequestParam(value = "lessonNumEnd",required = false) Integer lessonNumEnd,
                               @RequestParam(value = "practiceTypeId",required = false) Long practiceTypeId){
        Page<PracticePlan> iPage = new Page<>(pageNum, pageSize);

        Quarter quarterNow = iQuarterService.getNowQuarter();
        if (yearPart == null || quarterNum == null) {
            yearPart = quarterNow.getYearPart();
            quarterNum = quarterNow.getNum();
        }

        String schoolId = "";
        if (this.adminId.equals(1568138350324350975L)){
            schoolId = this.baseSchoolIdStr;
        }else {
            //主管管理的管区id
            /*String inSchoolId = iSchoolIntendantService.getSchoolIdsForTeacherId(yearPart,quarterNum,this.adminId,1);
            if (StringUtils.isBlank(inSchoolId)){
                return new Result(Code.GET_OK, new PageResult(iPage.getRecords(),iPage.getTotal(), iPage.getPages()), "获取成功");
            }
            schoolId = iSchoolQuarterService.getChilIds(yearPart,quarterNum,inSchoolId);
            if (StringUtils.isBlank(schoolId)){
                return new Result(Code.GET_OK, new PageResult(iPage.getRecords(),iPage.getTotal(), iPage.getPages()), "获取成功");
            }*/
            schoolId = "0";
        }
        iPracticeBrushTeacherService.getEnterList(iPage,yearPart,quarterNum,schoolId,lessonType,yearClassId,classTypeId,subjectsId,lessonNumStart,lessonNumEnd,practiceTypeId,teacherName,this.adminId);

        return new Result(Code.GET_OK, new PageResult(iPage.getRecords(),iPage.getTotal(), iPage.getPages()), "获取成功");
    }


    @PostMapping("")
    public Result savePracticeBrushTeacher(@RequestBody PracticeBrushTeacher practiceBrushTeacher) {
        if (StringUtils.isNotBlank(practiceBrushTeacher.getStr())) {
            String str = practiceBrushTeacher.getStr();
            String[] codes = str.split("、");
            Map<Character, Long> courseTypeMapping = new HashMap<>();
            courseTypeMapping.put('A', 1565023758494502914L);// 培优班
            courseTypeMapping.put('B', 1575859909042028546L);// 提高班
            courseTypeMapping.put('C', 1565023758494501025L);// 基础班
            courseTypeMapping.put('S', 1570701121454350337L);// 精英班

            for (String code : codes) {
                PracticeBrushTeacher teacher = new PracticeBrushTeacher();

                if (code.length() >= 2) {
                    int yearClassId = Integer.parseInt(code.substring(0, code.length() - 1));
                    teacher.setYearClassId(yearClassId);
                    char classType = code.charAt(code.length() - 1);
                    Long courseTypeId = courseTypeMapping.get(classType);
                    teacher.setTeacherId(practiceBrushTeacher.getTeacherId());
                    teacher.setYearPart(practiceBrushTeacher.getYearPart());
                    teacher.setQuarterNum(practiceBrushTeacher.getQuarterNum());
                    teacher.setSchoolId(practiceBrushTeacher.getSchoolId());
                    teacher.setSubjectsId(practiceBrushTeacher.getSubjectsId());
                    teacher.setTeacherType(practiceBrushTeacher.getTeacherType());
                    if (courseTypeId != null) {
                        teacher.setCourseTypeId(courseTypeId);
                    }
                    iPracticeBrushTeacherService.save(teacher);

                } else if (code.length() == 1) {
                    Long courseTypeId = null;
                    if (code.matches("^[0-9].*")) {
                        int yearClassId = Integer.parseInt(code.substring(0, code.length() - 1));
                        teacher.setYearClassId(yearClassId);
                    } else {
                        char classType = code.charAt(code.length() - 1);
                        courseTypeMapping.get(classType);
                    }
                    teacher.setTeacherId(practiceBrushTeacher.getTeacherId());
                    teacher.setYearPart(practiceBrushTeacher.getYearPart());
                    teacher.setQuarterNum(practiceBrushTeacher.getQuarterNum());
                    teacher.setSchoolId(practiceBrushTeacher.getSchoolId());
                    teacher.setSubjectsId(practiceBrushTeacher.getSubjectsId());
                    if (courseTypeId != null) {
                        teacher.setCourseTypeId(courseTypeId);
                    }
                    iPracticeBrushTeacherService.save(teacher);
                }
            }
        }

        return new Result(Code.SAVE_OK, null, Code.SAVE_OK_MSG);
    }


    @DeleteMapping("")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String")
    public Result deletePracticeBrushTeacher(@RequestParam(value = "id") String id) {
        PracticeBrushTeacher eduTask = iPracticeBrushTeacherService.getById(id);
        if (Objects.isNull(eduTask)) {
            return new Result(Code.EXP_ERROR, null, "该数据不存在");
        }
        iPracticeBrushTeacherService.removeById(id);
        return new Result(Code.DELETE_OK, null, Code.DELETE_OK_MSG);
    }
}
